package xyf.p10.dp;

import java.util.List;

/**
 * ClassName: P0120_MinPathSum
 * Description:
 * Author: xyf
 * Date: 9/9/22 10:27 AM
 * Version: 1.0
 **/
public class P0120_MinPathSum
{
    public int minimumTotal(List<List<Integer>> triangle)
    {
        int row = triangle.size();
        int col = triangle.get(row - 1).size();
        int[][] dp = new int[row][col];

        dp[0][0] = triangle.get(0).get(0);

        for (int i = 1; i < row; i++)
        {
            for (int j = 0; j <= i; j++)
            {
                if (j == 0)
                {
                    dp[i][j] = dp[i - 1][j] + triangle.get(i).get(j);
                }
                else if (j < i)
                {
                    dp[i][j] = Math.min(dp[i - 1][j], dp[i - 1][j - 1]) + triangle.get(i).get(j);
                }
                else
                {
                    dp[i][j] = dp[i - 1][j - 1] + triangle.get(i).get(j);
                }
            }
        }

        int min = Integer.MAX_VALUE;
        for (int j = 0; j < dp[0].length; j++)
        {
            min = Math.min(min, dp[dp.length - 1][j]);
        }
        return min;
    }
}